<?php
/**
 * Created by PhpStorm.
 * User: liyu
 * Date: 2018/4/9
 * Time: 下午4:24
 */

namespace app\common\model;


use app\api\model\ModelCode;
use think\Model;

class Base extends Model
{
    /**
     * 创建个性GUID
     * @param string $base_code
     * @return string
     */
    public function create_gid($base_code = '')
    {
        if (empty($base_code)) {
            $base_name = basename(str_replace('\\', '/', get_called_class()), '.php');
            $uuid_list = ModelCode::$gid_list;
            $base_code = isset($uuid_list[$base_name]) ? $uuid_list[$base_name] : 'QT';
        }
        $uuid = $base_code . strtoupper(uniqid()) . $this->builderRand(6);
        return $uuid;
    }

    /**
     * 创建随机数
     * @param int $num  随机数位数
     * @return string
     */
    public function builderRand($num=8){
        return substr(implode(NULL, array_map('ord', str_split(substr(uniqid(), 7, 13), 1))), 0, $num);
    }

    /**
     * 更新数据(重写)
     * @access public
     * @param array      $data  数据数组
     * @param array      $where 更新条件
     * @param array|true $field 允许字段
     * @return $this
     */
    public static function update($data = [], $where = [], $field = null)
    {
        $model = new static();
        if (!empty($field)) {
            $model->allowField($field);
        }
        $result = $model->isUpdate(true)->save($data, $where);
        return $result;
    }
}